function SylvekClient() {
}

SylvekClient.prototype.getRequest = function() {
	return new XMLHttpRequest();
}

SylvekClient.prototype.update = function(lat, lon, n, u, online, success, error) {
	try {
		var request = this.getRequest();
		request.open('POST', 'http://ou-android.appspot.com/rest/update');
		var o = "false";
		if(online) {
			o = "true";
		}
		
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if(	request.status == 200) {
					var result = JSON.parse(request.responseText); 
					if(result.success == true) {
	             		success("location updated");
					} else {
						error(request.responseText);
					}
	           	} else {
		           	error(request.responseText);
	           	}
	        }
		};
		
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.send("uid="+u+"&name="+name+"&latitude="+lat+"&longitude="+lon+"&online='"+o+"'");
	} catch (e) {
		error(e);
	}
}

SylvekClient.prototype.remove = function(u, success, error) {
	try {
		var request = this.getRequest();
		request.open('GET', 'http://ou-android.appspot.com/rest/delete?uid='+u);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
	           if (request.status == 200) {
	           		var result = JSON.parse(request.responseText);
					if(result.success == true) {
						success("location deleted");
					} else {
						error(request.responseText);
					}
	           } else {
					error(request.responseText);
	           }
			}
		};
		request.send();
	} catch (e) {
		error(e);
	}
}

SylvekClient.prototype.getPeople = function(url, success, error) {
	try {
		var request = this.getRequest();
		request.open('GET', url);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
	           if (request.status == 200) {
					var friends = JSON.parse(request.responseText);
					for(var i = 0 ; i < friends.length ; i ++) {
						getPerson(friends[i], success, error);
					}
	           } else {
					error(request.responseText);
	           }
			}
		};
		request.send();
	} catch (e) {
		error(e);
	}
}

SylvekClient.prototype.getPerson = function(p, success, error) {
	try {
		var request = this.getRequest();
		request.open('POST', 'http://ou-android.appspot.com/rest/friends');
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
	           if (request.status == 200) {
	        	   	var friends = JSON.parse(request.responseText);
					success(friends[0]);
	           } else {
					error(request.responseText);
	           }
			}
		};
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.send("uids="+p);
	} catch (e) {
		error(e);
	}
}